package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataTextArea extends DataGeometry implements ITag {
	public static String DEFAULT_ID = "_TextArea";

	DataText text = new DataText();
	DataFont font = new DataFont();

	DataColor fillColor = new DataColor(200,200,200);
	DataState border = new DataState(false);
	DataColor borderColor = new DataColor();

	DataPoint bottomRight = new DataPoint();
	DataPoint topRight = new DataPoint();
	DataPoint bottomLeft = new DataPoint();
	DataPoint endOfText = new DataPoint();
	DataSize lineHeight = new DataSize();

	DataState editable = new DataState();
	DataState focus = new DataState();

	GeometryText _textBlock = null;
	DataTranslation _translation;
public DataTextArea() {
	try{
		setId(DEFAULT_ID,this);

		getFilled().setValue(false,this);
		getBorder().setValue(false,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml) {

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.TEXT))
		{
			text = new DataText();
			text.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.FONT))
		{
			font = new DataFont();
			font.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.FILL_COLOR))
		{
			fillColor = new DataColor(200,200,200);
			fillColor.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BORDER))
		{
			border = new DataState();
			border.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BORDER_COLOR))
		{
			borderColor = new DataColor();
			borderColor.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BOTTOM_RIGHT))
		{
			bottomRight = new DataPoint();
			bottomRight.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.TOP_RIGHT))
		{
			topRight = new DataPoint();
			topRight.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.BOTTOM_LEFT))
		{
			bottomLeft = new DataPoint();
			bottomLeft.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.END_OF_TEXT))
		{
			endOfText = new DataPoint();
			endOfText.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.LINE_HEIGHT))
		{
			lineHeight = new DataSize();
			lineHeight.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.EDITABLE))
		{
			editable = new DataState(false);
			editable.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.FOCUS))
		{
			focus = new DataState(false);
			focus.buildFromgrfxML(element);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.TEXT_AREA,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml) {

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.TEXT))
		{
			text.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.FONT))
		{
			font.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.FILL_COLOR))
		{
			fillColor.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BORDER))
		{
			border.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BORDER_COLOR))
		{
			borderColor.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BOTTOM_RIGHT))
		{
			bottomRight.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.TOP_RIGHT))
		{
			topRight.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.BOTTOM_LEFT))
		{
			bottomLeft.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.END_OF_TEXT))
		{
			endOfText.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.LINE_HEIGHT))
		{
			lineHeight.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.EDITABLE))
		{
			editable.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.FOCUS))
		{
			focus.copyFromgrfxML(element);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.TEXT_AREA,""+element.getStartLineNo()} );
		}
	}
}
public DataState getBorder() {
	return border;
}
public DataColor getBorderColor() {
	return borderColor;
}
public DataPoint getBottomLeft() {
	return bottomLeft;
}
public DataPoint getBottomRight() {
	return bottomRight;
}
public DataGeometry getClone() {

	DataTextArea newTextArea = new DataTextArea();

	super.getClonedElements(newTextArea);

	newTextArea.text = text.getClone();
	newTextArea.font = font.getClone();

	newTextArea.fillColor = fillColor.getClone();
	newTextArea.border = border.getClone();
	newTextArea.borderColor = borderColor.getClone();

	newTextArea.bottomRight = bottomRight.getClone();
	newTextArea.topRight = topRight.getClone();
	newTextArea.bottomLeft = bottomLeft.getClone();
	newTextArea.endOfText = endOfText.getClone();
	newTextArea.lineHeight = lineHeight.getClone();

	newTextArea.editable = editable.getClone();
	newTextArea.focus = focus.getClone();

	return newTextArea;
}
public DataGeometry getClone(String path) {

	DataTextArea newTextArea = new DataTextArea();

	GrfxMLStoreProcessor.setCloneName( path,this,newTextArea );

	super.getClonedElements(path,newTextArea);

	newTextArea.text = text.getClone(path);
	newTextArea.font = font.getClone(path);

	newTextArea.fillColor = fillColor.getClone(path);
	newTextArea.border = border.getClone(path);
	newTextArea.borderColor = borderColor.getClone(path);

	newTextArea.bottomRight = bottomRight.getClone(path);
	newTextArea.topRight = topRight.getClone(path);
	newTextArea.bottomLeft = bottomLeft.getClone(path);
	newTextArea.endOfText = endOfText.getClone(path);
	newTextArea.lineHeight = lineHeight.getClone(path);

	newTextArea.editable = editable.getClone(path);
	newTextArea.focus = focus.getClone(path);

	return newTextArea;
}
public DataState getEditable() {
	return editable;
}
public DataPoint getEndOfText() {
	return endOfText;
}
public DataColor getFillColor() {
	return fillColor;
}
public DataState getFocus() {
	return focus;
}
public DataFont getFont() {
	return font;
}
public Geometry getGeometry(GrfxMLEngine engine) {

	if (_textBlock == null && _translation != null && color != null) {
		_textBlock = new GeometryText(
				engine,
				_translation.window,
				bounds,
				_translation.visibleView,
				transparency,
				color,
				filled,
				fillColor,
				border,
				borderColor,
				_translation.positionView,
				_translation.rotationView,
				offsetView,
				text,
				font,
				editable,
				focus,
				bottomRight,
				bottomLeft,
				topRight,
				endOfText,
				lineHeight);
		if( !NameHelper.isSystemName(getId()) )
		{
			_textBlock.setId(getId());
		}
	}

	return (_textBlock);
}
public DataSize getLineHeight() {
	return lineHeight;
}
public DataText getText() {
	return text;
}
public DataPoint getTopRight() {
	return topRight;
}
public DataTranslation getTranslation() {
	return _translation;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(text,this);
	}
	catch(LockException le)
	{}//Dummy catch
	text.register(store);

	try{
		store.add(font,this);
	}
	catch(LockException le)
	{}//Dummy catch
	font.register(store);

	try{
		store.add(fillColor,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(border,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(borderColor,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(bottomRight,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(bottomLeft,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(topRight,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(endOfText,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(lineHeight,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(focus,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(editable,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj,false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	_textBlock = null;

	super.replace(oldObj, newObj);

	if (text == oldObj)
		text = (DataText) newObj;

	if (font == oldObj)
		font = (DataFont) newObj;

	if (fillColor == oldObj)
		fillColor = (DataColor) newObj;

	if (border == oldObj)
		border = (DataState) newObj;

	if (borderColor == oldObj)
		borderColor = (DataColor) newObj;

	if (bottomRight == oldObj)
		bottomRight = (DataPoint) newObj;

	if (bottomLeft == oldObj)
		bottomLeft = (DataPoint) newObj;

	if (topRight == oldObj)
		topRight = (DataPoint) newObj;

	if (endOfText == oldObj)
		endOfText = (DataPoint) newObj;

	if (lineHeight == oldObj)
		lineHeight = (DataSize) newObj;

	if (focus == oldObj)
		focus = (DataState) newObj;

	if (editable == oldObj)
		editable = (DataState) newObj;

	if(recursive)
	{
		text.replace(oldObj, newObj,recursive);
	}
}
public void resetChanged() {
	super.resetChanged();

	text.resetChanged();
	font.resetChanged();
	fillColor.resetChanged();
	border.resetChanged();
	borderColor.resetChanged();
	bottomRight.resetChanged();
	bottomLeft.resetChanged();
	topRight.resetChanged();
	endOfText.resetChanged();
	focus.resetChanged();
	editable.resetChanged();
}
protected void setBorder(DataState newBorder) {
	border = newBorder;
}
protected void setBorderColor(DataColor newBorderColor) {
	borderColor = newBorderColor;
}
protected void setBottomLeft(DataPoint newBottomLeft) {
	bottomLeft = newBottomLeft;
}
protected void setBottomRight(DataPoint newBottomRight) {
	bottomRight = newBottomRight;
}
protected void setEditable(DataState newEditable) {
	editable = newEditable;
}
protected void setEndOfText(DataPoint newEndOfText) {
	endOfText = newEndOfText;
}
protected void setFillColor(DataColor newFillColor) {
	fillColor = newFillColor;
}
protected void setFocus(DataState newFocus) {
	focus = newFocus;
}
protected void setFont(DataFont newFont) {
	font = newFont;
}
protected void setLineHeight(DataSize newLineHeight) {
	lineHeight = newLineHeight;
}
protected void setText(DataText newText) {
	text = newText;
}
protected void setTopRight(DataPoint newTopRight) {
	topRight = newTopRight;
}
protected void setTranslation(DataTranslation newTranslation) {
	_translation = newTranslation;
}
public String togrfxML(String name) {
	String xml = "<" + name + ">";
	xml += super.togrfxML(name);
	xml += text.togrfxML(Token.TEXT);
	xml += font.togrfxML(Token.FONT);
	xml += fillColor.togrfxML(new DataColor(200,200,200),Token.FILL_COLOR);
	xml += border.togrfxML(Token.BORDER);
	xml += borderColor.togrfxML(Token.BORDER_COLOR);
	xml += bottomRight.togrfxML(Token.BOTTOM_RIGHT);
	xml += bottomLeft.togrfxML(Token.BOTTOM_LEFT);
	xml += topRight.togrfxML(Token.TOP_RIGHT);
	xml += endOfText.togrfxML(Token.END_OF_TEXT);
	xml += focus.togrfxML(Token.FOCUS);
	xml += editable.togrfxML(Token.EDITABLE);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t) {

	_translation = t;

//	font.translate(t);

	super.translate(t);
	font.translate(t);
}
}
